﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tasker.Composition;
using Tasker.Infrastructure.Repositories;
using Tasker.Repositories.Entities;
using Tasker.Infrastructure;
using Tasker.Infrastructure.Entities;

namespace Tasker.Services
{
    public class TaskService: ITaskService
    {
        private readonly ITasksRepository _tasks = Resolver.Resolve<ITasksRepository>();
        private readonly IUsersRepository _users = Resolver.Resolve<IUsersRepository>();
        private readonly ITaskWorkersRepository _taskWorkers = Resolver.Resolve<ITaskWorkersRepository>();
        private readonly IStatusRepository _status = Resolver.Resolve<IStatusRepository>();

        public ITask GetBlankEntity()
        {
            return _tasks.Create();
        }
        public void CompleteAllSubTasks(int taskId)
        {
            ITask task = _tasks.SelectById(taskId);
            if(task != null)
            {
                task.StatusId = 4;
                IEnumerable<ITask> tasks = _tasks.SelectSubTasks(taskId);
                foreach (var task1 in tasks)
                {
                    CompleteAllSubTasks(task1.Id);
                }
            }
        
        }
        public IEnumerable<ITask> SelectAllIncoming(int userId)
        {
            return _tasks.SelectByUserIncoming(userId);
        }

        public IEnumerable<ITask> SelectAllOutcoming(int userId)
        {
            return _tasks.SelectByUserOutcoming(userId);
        }

        public IStatus GetTaskStatus(int statusId)
        {
            return _status.SelectById(statusId);
        }

        public IEnumerable<IUser> GetTaskWorkers(int taskId)
        {
            IEnumerable<ITaskWorker> taskWorkers = _taskWorkers.SelectByTaskId(taskId);
            return from taskWorker in taskWorkers
                   select _users.SelectById(taskWorker.UserId);
        }

        public IUser GetTaskWorker(int taskId)
        {
            var res = GetTaskWorkers(taskId).ToArray();
            if (res.Count() < 1)
                return null;
            return res[0];
        }

        public ITask SelectTask(int userId, DateTime CreatedDate)
        {
            return _tasks.SelectByUserTime(userId, CreatedDate);
        }

        public void DeleteAllTaskWorkers(int taskId)
        {
            _taskWorkers.DeleteAll(taskId);
        }

        public void CreateTaskWorker(int taskId, int userId)
        {
            ITaskWorker taskWorker = _taskWorkers.Create();
            taskWorker.TaskId = taskId;
            taskWorker.UserId = userId;
            _taskWorkers.Insert(taskWorker);
        }

        public IEnumerable<ITask> SelectAll()
        {
            return _tasks.SelectAll();
        }

        public ITask SelectTask(int id)
        {
            return _tasks.SelectById(id);
        }

        public IEnumerable<ITask> SelectTasksByStatus(int statusId)
        {
            return null;
        }
        
        public IEnumerable<ITask> SelectUserTasks(int userId, int statusId = -1)
        {
            return null;
        }

        public ITask AddTask(ITask entity)
        {
            IStatus status = _status.GetStartStatus();
            entity.StatusId = status.Id;
            return _tasks.Insert(entity);
        }

        public void DeleteTask(int id)
        {
            ITask task = _tasks.SelectById(id);
            _tasks.Delete(task);
        }

        public void UpdateTask(ITask entity)
        {
            _tasks.Update(entity);
        }
    }
}
